using System;
using System.Web.UI.WebControls;

using Impact.Controllers;
using Impact.Views;
using Impact.Web.Foundation;
using Impact.Model.Objects;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for page to edit templates
    /// </summary>
    public partial class EditTemplate : PageBase, IPageEditorView
    {

        private EditPageController controller;

        /// <summary>
        /// override of onload to perform startup processing
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            controller = new EditPageController(this, UserId);
            controller.Initialize(!IsPostBack);

        }

        /// <summary>
        /// event handler for the edit plugins button
        /// </summary>
        protected void EditPluginsCommand(object sender, CommandEventArgs e)
        {
            Redirect(string.Format("EditPlugins.aspx?pid={0}&ReturnUrl={1}", Request.QueryString["pid"], Server.UrlEncode(Request.Url.ToString()) ));
        }

        /// <summary>
        /// event handler for the submit button
        /// </summary>
        protected void SubmitCommand(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                controller.SavePage();
                Redirect("Templates.aspx");
            }
        }

        /// <summary>
        /// event handler for the cancel button
        /// </summary>
        protected void CancelCommand(object sender, EventArgs e)
        {
            Redirect("Templates.aspx");
        }



        #region IPageEditorView Members

        /// <summary>
        /// uses the given object and fills all appropriate form fields with its data
        /// </summary>
        public void PopulateForm(ImpactPage page)
        {
            ViewState[ImpactPage.Columns.PageId] = page.PageId;

            name.Text = page.Caption;
            headerText.Text = page.Body;
            headerImage.Value = page.HeaderImage;
            footerText.Text = page.FooterText;
            footerImage.Value = page.FooterImage;
        }

        /// <summary>
        /// gets data out of the appropriate form fields and puts it into a page object
        /// </summary>
        public ImpactPage RetrieveFormData()
        {
            ImpactPage page = null;
            if (ViewState[ImpactPage.Columns.PageId] != null)
            {
                int pageId = (int)ViewState[ImpactPage.Columns.PageId];
                if (pageId > 0)
                {
                    page = controller.GetPage(pageId);
                }
            }

            if (page == null)
            {
                page = new ImpactPage();
                page.Type = PageType.Template.TypeId;
            }

            page.Caption = name.Text;
            page.Body = headerText.Text;
            page.HeaderImage = headerImage.Value;
            page.FooterText = footerText.Text;
            page.FooterImage = footerImage.Value;

            return page;

        }

        #endregion
    }
}
